package com.dy.lock.screen.utils;

/**
 * 计算位置信息
 * 
 * @author java
 * 
 */
public class LocationUtil {

	private static double EARTH_RADIUS = 6378137;

	public static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 根据经纬度计算距离(单位:米)
	 * 
	 * @param sourceLat
	 * @param sourceLng
	 * @param targetLat
	 * @param targetLng
	 * @return
	 */
	public static double calDistance(double sourceLat, double sourceLng,
			double targetLat, double targetLng) {
		double radLat1 = rad(sourceLat);
		double radLat2 = rad(targetLat);
		double a = radLat1 - radLat2;
		double b = rad(sourceLng) - rad(targetLng);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;

	}
}
